package com.letv.whatslive.model.convert;

import com.alibaba.fastjson.JSONObject;
import com.letv.whatslive.model.Dinggou;
import com.letv.whatslive.model.WeidianTokenInfo;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * Created by gaoshan on 15-7-13.
 */
public class WeidianTokenInfoConvert {


    public static DBObject castWeidianTokenInfoToDBObject(WeidianTokenInfo weidianTokenInfo) {
        DBObject dbo = new BasicDBObject();
        if (weidianTokenInfo.getId() != null) {
            dbo.put("_id", weidianTokenInfo.getId());
        }
        dbo.put("access_token", ObjUtils.toString(weidianTokenInfo.getAccess_token(), ""));
        dbo.put("expire_in", weidianTokenInfo.getExpire_in());
        dbo.put("refresh_token", ObjUtils.toString(weidianTokenInfo.getRefresh_token(), ""));
        dbo.put("openid", ObjUtils.toString(weidianTokenInfo.getOpenid(), ""));
        dbo.put("shop_name", ObjUtils.toString(weidianTokenInfo.getShop_name(), ""));
        dbo.put("scope", ObjUtils.toString(weidianTokenInfo.getScope(), ""));
        dbo.put("shop_logo", ObjUtils.toString(weidianTokenInfo.getShop_logo(), ""));
        dbo.put("createTime", ObjUtils.toLong(weidianTokenInfo.getCreateTime(), System.currentTimeMillis()));
        dbo.put("updateTime", ObjUtils.toLong(weidianTokenInfo.getUpdateTime(), System.currentTimeMillis()));
        dbo.put("status", weidianTokenInfo.getStatus() == null ? 1 : weidianTokenInfo.getStatus());

        if(weidianTokenInfo.getDinggou() != null) {
            DBObject dinggou = new BasicDBObject();

            dinggou.put("sellerId", weidianTokenInfo.getDinggou().getSellerId());
            dinggou.put("versionId", weidianTokenInfo.getDinggou().getVersionId());
            dinggou.put("id", weidianTokenInfo.getDinggou().getId());
            dinggou.put("version", weidianTokenInfo.getDinggou().getVersion());
            dinggou.put("endTime", weidianTokenInfo.getDinggou().getEndTime());
            dinggou.put("startTime", weidianTokenInfo.getDinggou().getStartTime());

            dbo.put("dinggou", dinggou);
        }

        return dbo;
    }

    public static WeidianTokenInfo castDBObjectToWeidianTokenInfo(DBObject dbObject) {
        WeidianTokenInfo weidianTokenInfo = new WeidianTokenInfo();
        if (dbObject != null) {
            weidianTokenInfo.setId(ObjUtils.toLong(dbObject.get("_id")));

            weidianTokenInfo.setAccess_token(ObjUtils.toString(dbObject.get("access_token")));
            weidianTokenInfo.setExpire_in(ObjUtils.toInteger(dbObject.get("expire_in")));
            weidianTokenInfo.setRefresh_token(ObjUtils.toString(dbObject.get("refresh_token")));
            weidianTokenInfo.setOpenid(ObjUtils.toString(dbObject.get("openid")));
            weidianTokenInfo.setShop_name(ObjUtils.toString(dbObject.get("shop_name")));
            weidianTokenInfo.setScope(ObjUtils.toInteger(dbObject.get("scope")));
            weidianTokenInfo.setShop_logo(ObjUtils.toString(dbObject.get("shop_logo")));
            weidianTokenInfo.setCreateTime(ObjUtils.toLong(dbObject.get("createTime")));
            weidianTokenInfo.setUpdateTime(ObjUtils.toLong(dbObject.get("updateTime")));
            Integer status = ObjUtils.toInteger(dbObject.get("status"));
            weidianTokenInfo.setStatus(status == null ? 1 : status);

            if(dbObject.get("dinggou") != null) {
                BasicDBObject basicDBObject = (BasicDBObject) dbObject.get("dinggou");

                Dinggou dinggou = new Dinggou();
                dinggou.setSellerId(ObjUtils.toString(basicDBObject.get("sellerId")));
                dinggou.setVersionId(ObjUtils.toInteger(basicDBObject.get("versionId")));
                dinggou.setId(ObjUtils.toInteger(basicDBObject.get("id")));
                dinggou.setEndTime(basicDBObject.getString("endTime"));
                dinggou.setStartTime(basicDBObject.getString("startTime"));

                weidianTokenInfo.setDinggou(dinggou);
            }
        } else {
            return null;
        }
        return weidianTokenInfo;
    }


    public static WeidianTokenInfo castVDianObjectToWeidianTokenInfo(JSONObject vdianResult) {
        WeidianTokenInfo weidianTokenInfo = new WeidianTokenInfo();
        if (vdianResult != null) {
            weidianTokenInfo.setAccess_token(vdianResult.getString("access_token"));
            weidianTokenInfo.setExpire_in(vdianResult.getIntValue("expire_in"));
            weidianTokenInfo.setRefresh_token(vdianResult.getString("refresh_token"));
            weidianTokenInfo.setOpenid(vdianResult.getString("openid"));
            weidianTokenInfo.setShop_name(vdianResult.getString("shop_name"));
            weidianTokenInfo.setScope(vdianResult.getIntValue("scope"));
            weidianTokenInfo.setShop_logo(vdianResult.getString("shop_logo"));
        } else {
            return null;
        }
        return weidianTokenInfo;
    }

}
